package com.zou.lotterylucky.activity

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.zou.lotterylucky.Constants
import com.zou.lotterylucky.R
import com.zou.lotterylucky.TimeSyncService
import com.zou.lotterylucky.data.MoneyLimit
import com.zou.lotterylucky.data.Order
import com.zou.lotterylucky.data.TimeLimit
import com.zou.lotterylucky.utils.LangUtil
import com.zou.lotterylucky.utils.MyContextWrapper
import com.zou.lotterylucky.utils.SPUtils
import com.zou.lotterylucky.utils.ToastUtil

/**
 * activity基类
 */
open class BaseActivity : Activity() {

    var belongTo = mutableListOf<Int>()

    companion object {
        lateinit var timeLimits: MutableList<TimeLimit>
        lateinit var moneyLimits: MutableList<MoneyLimit>
        lateinit var selectedOrder: Order
        lateinit var allOrders: MutableList<Order>
        lateinit var deletedOrders: MutableList<Order>
        lateinit var todayOrders: MutableList<Order>
        lateinit var todayBets: MutableList<BetCountEntity>
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val timeSyncServiceName = TimeSyncService::class.qualifiedName.toString()
        if (!isServiceRunning(timeSyncServiceName)) {
            Log.d("BaseActivity", "服务不在运行")
            startService(Intent(this, TimeSyncService::class.java))
        }
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(
            MyContextWrapper.wrap(
                newBase,
                LangUtil.getLocaleByLanguage((SPUtils[newBase!!, Constants.SP_Lang, 1] as Int).toString())
            )
        )
    }

    fun isTimeLimit(belongTo: String): Boolean {
        if (belongTo.contains("K") && !timeLimits[0].isEnable()) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }

        if (belongTo.contains("F") && !timeLimits[1].isEnable()) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }

        if (belongTo.contains("D") && !timeLimits[2].isEnable()) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }

        if (belongTo.contains("S") && !timeLimits[3].isEnable()) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }
        return true
    }

    fun editTimeLimit(belongTo: String): String {
        if (belongTo.contains("K") && !timeLimits[0].isEnable()) {
            belongTo.replace("K", "")
        }

        if (belongTo.contains("F") && !timeLimits[1].isEnable()) {
            if (belongTo.contains("K")) {
                belongTo.replace("+F", "")
            } else {
                belongTo.replace("F", "")
            }
        }

        if (belongTo.contains("D") && !timeLimits[2].isEnable()) {
            if (belongTo.contains("K") || belongTo.contains("F")) {
                belongTo.replace("+D", "")
            } else {
                belongTo.replace("D", "")
            }
        }

        if (belongTo.contains("S") && !timeLimits[3].isEnable()) {
            if (belongTo.length >= 3) {
                belongTo.replace("+S", "")
            } else {
                belongTo.replace("S", "")
            }
        }

        return belongTo
    }

    fun deleteTimeLimit(deal: Order): Boolean {
        val orderTime = deal.timeStrAuto
        if (deal.belongTo.contains("K") && !timeLimits[0].deleteEnable(orderTime)) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }
        if (deal.belongTo.contains("F") && !timeLimits[1].deleteEnable(orderTime)) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }
        if (deal.belongTo.contains("D") && !timeLimits[2].deleteEnable(orderTime)) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }
        if (deal.belongTo.contains("S") && !timeLimits[3].deleteEnable(orderTime)) {
            ToastUtil.showToast(this, R.string.can_not_edit)
            return false
        }
        return true
    }

    fun isNetworkAvailable(context: Context?): Boolean {
        if (context == null) return false
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val capabilities =
            connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
        if (capabilities != null) {
            when {
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
                    return true
                }
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
                    return true
                }
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> {
                    return true
                }
            }
        }
        return false
    }

    /**
     * 判断服务是否在运行
     * @param serviceName 服务名称为全路径 例如com.ghost.WidgetUpdateService
     */
    fun isServiceRunning(serviceName: String): Boolean {
        val manager = getSystemService(AppCompatActivity.ACTIVITY_SERVICE) as ActivityManager
        for (service in manager.getRunningServices(Int.MAX_VALUE)) {
            if (serviceName == service.service.className) {
                return true
            }
        }
        return false
    }
}